﻿// -----------------------------------------------------------------------
// <copyright file="PosService.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super.Common;
using Super.Infrastructure;

namespace Super.DomainService
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class PosService
    {
        private readonly DbContextAdapter _adp;
        private readonly StockService _stockService;
        public PosService( DbContextAdapter adp)
        {
            _adp = adp;
            _stockService=new StockService(_adp);
        }

        public string GetOrderNo(int WorkerId)
        {
            var orderNo = CacheService.GetOneCache(WorkerId.ToString() + "pos");
            if (string.IsNullOrEmpty(orderNo))
            {
                orderNo = Number.GetSerialNumber().ToString();
                CacheService.SetOneCache(WorkerId.ToString() + "pos", orderNo);
            }
            return orderNo;
        }

        public int GetBuyUserId(int WorkerId)
        {
            var userId = CacheService.GetOneCache(WorkerId.ToString() + "pos_buyer");
            return userId ?? 0;
        }

      

        public void SetBuyUserId(int WorkerId,int BuyerId)
        {
            CacheService.SetOneCache(WorkerId.ToString() + "pos_buyer",BuyerId);
        }

        public void RestBuyUserId(int WorkerId)
        {
            CacheService.OneCache.Remove(WorkerId.ToString() + "pos_buyer");
        }

        public void Clear(int WorkerId)
        {
            var orderno = GetOrderNo(WorkerId);
            CacheService.OneCache.Remove(WorkerId.ToString() + "pos");
            CacheService.SellStockCache.Remove(orderno);
            RestBuyUserId(WorkerId);
        }

        /// <summary>
        /// 设置 销售项
        /// </summary>
        /// <param name="WorkerId"></param>
        /// <param name="StockId"></param>
        public void SetSellItems(int WorkerId,int StockId)
        {
            var orderNo = GetOrderNo(WorkerId);
            List<SellItems> sellList = CacheService.GetSellItems(orderNo);
             var sellModel = sellList.FirstOrDefault(a => a.StockId == StockId);
             if (sellModel != null)
             {
                 sellModel.Count += 1;
             }
             else
             {
                 var stockModel = _stockService.Repository.Find(a => a.ID == StockId).FirstOrDefault();
                 if (stockModel != null)
                 {
                     var stockItem = new SellItems
                                         {
                                             Count = 1,
                                             Price = stockModel.LSPrice,
                                             ProductName = stockModel.Product.Title,
                                             StockId = stockModel.ID
                                         };
                     sellList.Add(stockItem);
                 }
             }
             CacheService.SetSellItems(orderNo, sellList);
        }

        /// <summary>
        /// 编辑购物车中的产品数量
        /// </summary>
        /// <param name="WorkerId"></param>
        /// <param name="StockId"></param>
        /// <param name="Count"></param>
        public void EditSellItems(int WorkerId,int StockId,int Count)
        {
            var orderNo = GetOrderNo(WorkerId);
            List<SellItems> sellList = CacheService.GetSellItems(orderNo);
            var sellModel = sellList.FirstOrDefault(a => a.StockId == StockId);
            if (sellModel != null)
            {
                sellModel.Count = Count;
                CacheService.SetSellItems(orderNo, sellList);
            }
           
        }

        public List<SellItems> GetSellItems(int WorkerId)
        {
            var orderNo = GetOrderNo(WorkerId);
            return CacheService.GetSellItems(orderNo);
        }
    }
}
